//向服务器发送http请求
function request(url,method,body,callback){
	var xmlhttp;
	if(window.XMLHttpRequest){
		xmlhttp = new XMLHttpRequest();
	}else{
		xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
	}
	xmlhttp.onreadystatechange = function(){
		if(xmlhttp.readyState==4 && xmlhttp.status==200){
			callback(xmlhttp.responseText);
		}
	};
	xmlhttp.open(method,url,true);
	xmlhttp.setRequestHeader("Content-Type","application/json");
	xmlhttp.send(body);
}

//通过id查找html标签，并设置标签内容为指定文本
function setHtml(id,html){
	document.getElementById(id).innerHTML = html;
}

var showProduct = function(res){
	var obj = JSON.parse(res);
	console.log(obj);
	var text = '';
	for(var i=0;i<obj.data.length;i++){
		var each = obj.data[i];
		text += 
		'<li class="col-md-4 col-xs-6">\
			<div class="hwj_sMain_inner">\
				<div class="hwj_sMain1-1">\
					<a href="#">\
						<img src="'+each.url+'">\
					</a>\
				</div>\
				<div class="hwj_sMain1-2">\
					<a href="'+'">'+each.name+'</a>\
					<span>¥'+each.price+'</span>\
					<button>ADD TO CART</button>\
				</div>\
			</div>\
		</li>\n';
	}
	setHtml("list",text);
	//console.log(res);
};

function addCookie(key,value){
	var date = new Date();
	date.setTime(date.getTime()+1000*60*60*24);
	var expires = "expires="+date.toGMTString();
	document.cookie=key+"="+value+";"+expires;
}
/* var addCookie = function (name, value) {
    var strSec = getSec(time);
    var exp = new Date();
    exp.setTime(exp.getTime() + );    
    //设置cookie的名称、值、失效时间
    document.cookie = name + "=" + value + ";expires="+ exp.toGMTString();  
} */